package comc.springbatch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.step.builder.AbstractTaskletStepBuilder;
import org.springframework.batch.core.step.builder.SimpleStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.Arrays;
import java.util.List;

@Configuration
@EnableBatchProcessing
@EnableScheduling
public class BatchTest {
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
public MyDecider myDecider;

    @Bean
    public Job job(){
        return jobBuilderFactory.get("job22")
                .start(step()).listener(new MyJobListener()).build();
    }
    @Bean
    public Step step() {
        SimpleStepBuilder<Object, Object> step22 = stepBuilderFactory.get("step22").
                chunk(2)
                .faultTolerant()
                .listener(new MyChunkListiener())
                .reader(myReader())
                .writer(myWriter());
        return step22
                .build();
    }
    private ItemWriter<? super Object> myWriter() {
        return new ItemWriter<Object>() {
            @Override
            public void write(List<?> list) throws Exception {
                for (Object o : list) {
                    System.out.println("写入数据"+o.toString());
                }
            }
        };
    }
    private ItemReader<String> myReader() {
        return new ListItemReader<>(Arrays.asList("java课程","PHP课程"));
    }
}
